package tt.dz.egobus.servicemanager.business.distribute;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tt.dz.egobus.api.exception.ResponseException;
import tt.dz.egobus.api.managerservice.manager.RequestTask;
import tt.dz.egobus.api.managerservice.manager.UserRequestTypeInfo;
import tt.dz.egobus.bean.BusinessStatus;
import tt.dz.egobus.servicemanager.bean.AbstractGrabApplicationContext;
import tt.dz.egobus.servicemanager.business.point.BusPoint;
import tt.dz.egobus.servicemanager.core.exception.BusinessException;
import tt.dz.egobus.servicemanager.entities.DriverRegisterBusRelative;
import tt.dz.egobus.servicemanager.service.BusinessServiceService;
import tt.dz.egobus.servicemanager.service.DriverRegisterBusRelativeService;
import tt.dz.egobus.servicemanager.service.GrabBusinessCacheService;
import tt.dz.egobus.servicemanager.service.GrabRedisBusinessWorkCacheService;
import tt.dz.egobus.servicemanager.vo.BusinessRequestVo;

import static tt.dz.egobus.bean.I18NCodeEnum.RPC_SYSTEM_CODE_ERROR;
import static tt.dz.egobus.bean.ServiceManagerI18NCodeEnum.RPC_CREATE_BUSINESS_SERVICE_ERROR;

/**
 * @author Anson
 * @date 17-8-30
 * @name 及时包车系统派单
 * @since 1.0.7
 */
public class RightNowSystemDistributeBusinessContext extends AbstractGrabApplicationContext
        implements SystemDistributeBusinessContext {

    private static final Logger LOGGER = LoggerFactory.getLogger(RightNowSystemDistributeBusinessContext.class);

    private final DriverRegisterBusRelative relative;
    private final BusPoint busPoint;

    public RightNowSystemDistributeBusinessContext(DriverRegisterBusRelative relative, BusPoint busPoint) {
        this.relative = relative;
        this.busPoint = busPoint;
    }

    @Override
    public UserRequestTypeInfo.UserRequestType getRequestType() {
        return UserRequestTypeInfo.UserRequestType.NOW;
    }

    @Override
    public boolean handleSystemDistributeBusiness(BusinessRequestVo businessRequestVo,
                                                  String businessId, String driverId) throws BusinessException {
        GrabRedisBusinessWorkCacheService grabBusinessWorkCacheService = getGrabRedisBusinessWorkCacheService();
        DriverRegisterBusRelativeService driverRegisterBusRelativeService = this.getDriverRegisterBusRelativeServiceReference();
        BusinessServiceService businessServiceService = this.getBusinessServiceServiceReference();
        GrabBusinessCacheService grabBusinessCacheService = this.getBusinessCacheServiceReference();
        RequestTask requestTask = businessRequestVo;
        boolean isDone = false;
        try {
            //更新当前司机的状态
            isDone = grabBusinessWorkCacheService.doDriverBeginBusiness(driverId);
            LOGGER.info("调用者确认某个司机进行派单分发->更新当前司机的状态为={}", isDone);
            if (isDone) {
                //更新当前司机绑定车辆状态
                isDone = driverRegisterBusRelativeService.doUpdateBeginBusiness(relative, businessId);
                LOGGER.info("开始业务->当前更新当前司机绑定车辆状态={}", isDone);
                if (!isDone) {
                    //事务回滚
                    throw new ResponseException(RPC_CREATE_BUSINESS_SERVICE_ERROR);
                }
                //创建一个业务流水数据库
                isDone = businessServiceService.doSaveBeginBusiness(relative,
                        businessId, requestTask.getUserId(), busPoint, businessRequestVo);
                if (!isDone) {
                    //事务回滚
                    throw new ResponseException(RPC_SYSTEM_CODE_ERROR);
                }
                //若是当前的处理结果成功，则确认当前分配的结果正确性
                //则在缓存业务中需要删除当前对应的缓存
                grabBusinessCacheService.updateStatus(businessId, BusinessStatus.BEGIN_BUSINESS);
            }
        } finally {
            // todo nothing
        }
        return isDone;
    }
}
